package com.sumainfor.pc.market.service;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.RdHousingDepMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Describe 房源描述模板
 */
@Service
public class RdHousingDepService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(RdHousingDepService.class);// 日志

    @Autowired
    RdHousingDepMapper rdHousingDepMapper;
    @Autowired
    UtilsMapper utilsMapper;

    private String UUID=null;
    /**
     * 查询房源描述模板
     */
    public List<Map<String,Object>> getHousingDepList(){
        return rdHousingDepMapper.getHousingDepList();
    }
    /**
     * 创建房源描述模板
     */
    public Integer addHousingDep(Map<String,Object> params){
        Integer result=0;
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                //房源描述模板编号
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "DepId");
                    put("database", Constants.DATA_SOURCE_TCMKT);
                    put("surface", "tcmkt_housing_dep");
                }});
                if(uuid!=null &&(!uuid.get("uuid").toString().equals(""))){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("de",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("de",0);
                }
                params.put("DepId",UUID);//房源描述编号
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
               result=rdHousingDepMapper.addHousingDep(params);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
